<?php

/**
 * @Author: 刘洪
 * @Date:   2018-11-20 16:03:51
 * @Last Modified by:   user
 * @Last Modified time: 2018-12-05 11:56:17
 */
namespace app\common\remind;

use app\common\remind\RemindMain;
use think\Db;

/**
 * 
 * 生日提醒类
 */
class RemindKehuqr extends RemindMain
{
	/**
	 * 设置提醒类型为生日提醒
	 * @var integer
	 */
	protected $remindType=4;

	/**
	 * 实例入口
	 * @return [type] [description]
	 */
	public function index(){

		$this->remindList=parent::init();

			//数据重组
		$this->regroupData();

			//发送提醒
		parent::send('客户确认');
	}

	/**
	 * 实现父类抽象方法
	 * @access protected
	 * @return [type] [description]
	 */
	protected function regroupData()
	{
		//arr[]=[trigger,send_type,receives,send_connect]
		//		[触发类型[]，发送方式，接收人[]，发送内容[]]
		foreach ($this->remindList as $arr) {
			switch ($arr['trigger']) {
				case 0:
					$where="TIMESTAMPDIFF(MINUTE,FROM_UNIXTIME(`qr_time`),now())<=".$this->period*60;
					break;
			}
			if(isset($where)){
				$kehuqr=Db::table('cmf_qrkh')->where($where)->select();
				//格式化数据
				$this->formatSendList($kehuqr,$arr);
			}
		}
	}

	/**
	 * 数据格式化
	 * @access protected
	 * @param  array $birthday [description]
	 * @param  array $arr      [description]
	 * @return [type]           [description]
	 */
	protected function formatSendList($kehuqr,$arr)
	{
	//arr[]=[trigger,send_type,receives,send_connect]
			//[触发类型，发送方式，接收人，发送内容]
		
		foreach ($kehuqr as $it) {
			$sms_connect="";
			//发送内容
			$send_connect="<div style='background-color:#ccc;margin-top:10px;'><h3>客户确认签字提醒</h3>";
			
			//send_connect=[0姓名，1生日，2部门，3职位]
			foreach ($arr['send_connect'] as $k) {
				if($k==0){
					$send_connect.="<div>姓名：".$it['kh_name']."</div>";
					$sms_connect.="姓名：".$it['kh_name']." ";
				}
				if($k==1){
					$send_connect.="<div>标题：".$it['title']."</div>";
					$sms_connect.="标题：".$it['title']." ";
				}
				if($k==2){
					$send_connect.="<div>确认时间：".date("Y-m-d H:i:s",$it['qr_time'])."</div>";
				}
			}
			$send_connect.="</div>";
			$sms_connect.=" 已确认签字，请留意;";

			//接收人 
			foreach ($arr['receives'] as $k) {
				$user=Db::name('user')->where('id='.$k)->find();
				//邮箱
				if($arr['send_type']==0 || $arr['send_type']==1){
					if(!isset($this->sendList['email'][$user['user_email']])){
						$this->sendList['email'][$user['user_email']]="";
					}
					$this->sendList['email'][$user['user_email']].=$send_connect;
				}
				//短信
				if($arr['send_type']==0 || $arr['send_type']==1){
					if(!isset($this->sendList['mobile'][$user['mobile']])){
						$this->sendList['mobile'][$user['mobile']]="";
					}
					$this->sendList['mobile'][$user['mobile']].=$sms_connect;
				}
			}
		}

	}





}